package com.legendshop.openportal.sdk.service;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.legendshop.openportal.sdk.model.OpenPageSupport;
import com.legendshop.openportal.sdk.model.OpenResult;
import com.legendshop.openportal.sdk.model.constants.OrderDetailTypeEnum;
import com.legendshop.openportal.sdk.model.request.*;
import com.legendshop.openportal.sdk.model.request.v4.OrderDetailV4Request;
import com.legendshop.openportal.sdk.model.request.v4.OrderPageV4Request;
import com.legendshop.openportal.sdk.model.request.v4.OrderTrackV4Request;
import com.legendshop.openportal.sdk.model.response.*;
import com.legendshop.openportal.sdk.model.response.prod.ProdPromiseInfoResponse;
import com.legendshop.openportal.sdk.model.response.v4.*;

import java.util.List;

/**
 * 开放平台订单服务
 * @author jzh
 * @version 1.0.0
 * @title OrderService
 * @date 2024/6/24 9:59
 * @description：
 */
public class OrderService extends BaseService {

    /**
     * 获取运费
     * @param request
     * @return
     */
    public static OpenResult<OrderFreightResponse> getFreight(OrderFreightRequest request) {
        return execute(request, OrderFreightResponse.class);
    }

    /**
     * 查询配送预计送达时间
     * @param request
     * @return
     */
    public static OpenResult<ProdPromiseInfoResponse> getPromiseInfo(ProdPromiseInfoRequest request) {
        return execute(request, ProdPromiseInfoResponse.class);
    }

    /**
     * 提交订单
     * @param submitOrderRequest
     * @return
     */
    public static OpenResult<SubmitOrderResponse> submitOrder(SubmitOrderRequest submitOrderRequest) {
        return execute(submitOrderRequest, SubmitOrderResponse.class);
    }


    /**
     * 支付后，确认订单
     * @param request
     * @return
     */
    public static OpenResult<Void> confirmOrder(OrderConfirmRequest request) {
        return execute(request, Void.class);
    }

    /**
     * 批量确认是否取消
     * @param request
     * @return
     */
    public static OpenResult<List<BatchCheckCancelResponse>> batchCheckCancel(BatchCheckCancelRequest request) {
        return execute(request, new TypeReference<List<BatchCheckCancelResponse>>() {});
    }

    /**
     * 取消订单
     * @param request
     * @return
     */
    public static OpenResult<Void> cancelOrder(OrderCancelRequest request) {
        return execute(request, Void.class);
    }

    /**
     * 查询订单详情
     * @param request
     * @return
     */
    public static OpenResult<OrderDetailResponse> orderDetail(OrderDetailRequest request) {
        OpenResult<JSONObject> openResult = execute(request, JSONObject.class);
        if (!openResult.isSuccess()) {
            return OpenResult.fail(openResult);
        }

        JSONObject jsonObject = openResult.getData();
        Integer type = jsonObject.getInt("type");
        OrderDetailResponse baseResponse = null;
        if (OrderDetailTypeEnum.PARENT_ORDER.getValue().equals(type)) {
            baseResponse = JSONUtil.toBean(jsonObject, ParentOrderDetailResponse.class);
        } else if (OrderDetailTypeEnum.SUB_ORDER.getValue().equals(type)) {
            baseResponse = JSONUtil.toBean(jsonObject, SubOrderDetailResponse.class);
        } else {
            baseResponse = JSONUtil.toBean(jsonObject, OrderDetailResponse.class);
        }
        return OpenResult.success(baseResponse);
    }

    /**
     * 查询订单物流
     * @param request
     * @return
     */
    public static OpenResult<OrderTrackResponse> orderTrack(OrderTrackRequest request) {
        return execute(request, OrderTrackResponse.class);
    }

    /**
     * 确认收货
     * @param request
     * @return
     */
    public static OpenResult<Void> confirmReceived(OrderConfirmReceivedRequest request) {
        return execute(request, Void.class);
    }

    //**********************************V4部分**************************************

    /**
     * 订单详情
     * @param request
     * @return
     */
    public static OpenResult<OrderDetailV4Response> orderDetailV4(OrderDetailV4Request request) {
        OpenResult<JSONObject> openResult = execute(request, JSONObject.class);
        if (!openResult.isSuccess()) {
            return OpenResult.fail(openResult);
        }

        JSONObject jsonObject = openResult.getData();
        Integer type = jsonObject.getInt("type");
        OrderDetailV4Response baseResponse = null;
        if (OrderDetailTypeEnum.PARENT_ORDER.getValue().equals(type)) {
            baseResponse = JSONUtil.toBean(jsonObject, ParentOrderDetailV4Response.class);
        } else if (OrderDetailTypeEnum.SUB_ORDER.getValue().equals(type)) {
            baseResponse = JSONUtil.toBean(jsonObject, SubOrderDetailV4Response.class);
        } else {
            baseResponse = JSONUtil.toBean(jsonObject, OrderDetailV4Response.class);
        }
        return OpenResult.success(baseResponse);
    }

    /**
     * 订单列表
     * @param request
     * @return
     */
    public static OpenResult<OpenPageSupport<MyParentSubV4Response>> orderPageV4(OrderPageV4Request request) {
        return execute(request, new TypeReference<OpenPageSupport<MyParentSubV4Response>>() {});
    }

    /**
     * 订单物流
     * @param request
     * @return
     */
    public static OpenResult<OrderTrackV4Response> orderTrackV4(OrderTrackV4Request request) {
        return execute(request, OrderTrackV4Response.class);
    }
}
